/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.table.renderer;

import org.openeccos.table.BaseTableModel;
import org.openeccos.util.PDFormat;
import org.openeccos.widgets.PDLabel;
import org.openeccos.widgets.PDNumericField;
import org.openeccos.widgets.PDTextField;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Component;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Table;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.table.TableCellRenderer;
import echopointng.TextFieldEx;

/**
 * @author Alejandro Salas
 * <br> Created on Feb 5, 2007
 */
public class TextRenderer implements TableCellRenderer {

	private boolean editable;
	private boolean isNumber;
	private boolean isInteger;
	private int maxLength;
	private boolean bold;

	public TextRenderer(boolean editable, boolean isNumber) {
		this(editable, isNumber, 50);
	}

	public TextRenderer(boolean editable, boolean isNumber, boolean bold) {
		this(editable, isNumber);
		this.bold = bold;
	}

	public TextRenderer(boolean editable, boolean isNumber, int maxLength) {
		this.editable = editable;
		this.isNumber = isNumber;
		this.maxLength = maxLength;
	}

	public Component getTableCellRendererComponent(Table table, Object value, final int col, final int row) {
		final BaseTableModel tableModel = (BaseTableModel) table.getModel();

		String strValue = (value == null ? "" : value.toString());

		if (editable) {
			final TextFieldEx txt;
			if (isNumber) {
				txt = new PDNumericField();
				if (isInteger) {
					((PDNumericField) txt).setNumberFormat(PDFormat.getNumberFormat0());
				}
				((PDNumericField) txt).setNumber(((Number) value).doubleValue());
			} else {
				txt = new PDTextField(strValue);
			}
			txt.setWidth(new Extent(100, Extent.PERCENT));
			txt.setMaximumLength(maxLength);
			txt.setActionCausedOnChange(true);
			txt.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					txtChanged(txt, col, row, tableModel);
				}
			});
			return txt;
		}

		if (strValue.length() == 0) {
			strValue = "..."; // (otherwise it will not be shown properly in PnlHistory)
		}

		PDLabel ret = new PDLabel(strValue, bold ? PDLabel.HEADER_3 : PDLabel.TABLE_DATA_LAST);
		ret.setInsets(new Insets(0));
		ret.setOutsets(new Insets(1, 0, 1, 0));
		if (isNumber) {
			ret.setTextAlignment(new Alignment(Alignment.RIGHT, Alignment.DEFAULT));
		}
		return ret;
	}

	protected boolean isValid(String value, int col, int row) {
		// Override if needed. Return true to
		// keep the value, false to drop it
		return true;
	}

	public boolean isInteger() {
		return isInteger;
	}

	public void setInteger(boolean isInteger) {
		this.isInteger = isInteger;
	}

	private void txtChanged(TextFieldEx txt, int col, int row, BaseTableModel tableModel) {
		if (isValid(txt.getText(), col, row)) {
			Object value = isNumber ? ((PDNumericField) txt).getNumber() : txt.getText();
			tableModel.setValueAt(value, col, row);
		} else {
			Object value = tableModel.getValueAt(col, row);
			if (value instanceof Number) {
				((PDNumericField) txt).setNumber(((Number) value).doubleValue());
			} else {
				txt.setText(value.toString());
			}
		}
	}
}